package com.sysu.minsysu.zxcx;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sysu.R;
import com.sysu.minsysu.zxcx.NewsNetwork.LinkNode;

public class NewsMain extends Activity{
	private static final String TAG="news";
	ArrayList<LinkNode> mLinkNodes;
	LinearLayout mLayout;
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newsmain);
		mLayout = (LinearLayout)findViewById(R.id.layout);		
		
		mLinkNodes = NewsNetwork.getNews();
		if( mLinkNodes==null ){
			Toast.makeText(this, "网络连接出错，请检查", Toast.LENGTH_SHORT).show();
			return;
		}
		Log.v(TAG, "" + mLinkNodes.size());
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		for( LinkNode node : mLinkNodes ){
			ArticleTitleItem titie = new ArticleTitleItem(this, node.title);
			titie.setTag(node);
			//TextView tv = new TextView(this);
			//tv.setLayoutParams(layoutParams);
			//tv.setText(node.title);
			//tv.setTag(node);
			titie.setOnClickListener( new OnTitleClickListener(titie));
			mLayout.addView(titie);
		}
	}
	
	class OnTitleClickListener implements OnClickListener{
		ArticleTitleItem mItem;
		public OnTitleClickListener( ArticleTitleItem item ){
			mItem = item;
		}
		@Override
		public void onClick(View v) {
			mItem.setBeenRead();
			LinkNode node = (LinkNode)v.getTag();
			Intent intent = new Intent(NewsMain.this, ShowArticle.class);
			intent.putExtra("linknode", node);
			startActivity(intent);
		}
	}

}
